<template>
  <div class="menu-maintenance">
    <!-- 角色维护 -->
    <section class="layer">
      <div class="box">
        <div class="box-header handle">
          <span class="box-title">菜单维护</span>
          <div class="box-tools">
            <el-button @click="restSearch">重置</el-button>
            <el-button type="success" @click="saveRoleInfo">保存</el-button>
          </div>
        </div>

        <div class="box-body">
          <el-form ref="searchForm" :model="searchForm" :rules="rules" label-width="80px">
            <el-row :gutter="12">
              <el-col :span="24">
                <el-form-item label="角色名称" prop="roleName">
                  <el-input v-model="searchForm.roleName" clearable placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="12">
              <el-col :span="24">
                <el-form-item label="描述信息" prop="desc">
                  <el-input v-model="searchForm.dscr" :resize="none" :rows="5" clearable placeholder="请输入" type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </section>
    <!-- 表格 -->
    <section class="layer">
      <div class="box">
        <div class="box-header handle">
          <span class="box-title">管理员列表</span>
          <div class="box-tools">
            <el-button type="success" @click="addNewUser">新增</el-button>
            <el-button type="danger" @click="delUser">删除</el-button>
          </div>
        </div>
        <div class="box-body">
          <el-table
            v-loading="tableLoading"
            :data="tableData"
            height="string"
            element-loading-spinner="el-loading1"
            highlight-current-row
            style="width: 100%"
            border
            fit
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" align="center" width="50" />
            <el-table-column label="序号" type="index" align="center" width="50" />
            <el-table-column prop="uact" show-overflow-tooltip label="用户账号" align="center" />
            <el-table-column prop="userName" show-overflow-tooltip label="姓名" align="center" />
            <el-table-column prop="uactStas" show-overflow-tooltip label="账号状态" align="center">
              <template slot-scope="scope">
                <!-- {{ scope.row.uactStas==="1"?'启用':'禁用' }} -->

                <el-switch
                  v-model="scope.row.uactStas"
                  disabled
                  active-value="1"
                  inactive-value="3"
                  @change="switchChange(scope.$index,scope.row)"
                />

              </template>
            </el-table-column>
            <el-table-column prop="dscr" label="描述信息" align="center" width="400" />
          </el-table>
        </div>
      </div>
    </section>
    <SecurityUser v-if="dataObj.isShow" :data-obj="dataObj" @cancelDialog="cancelDialog"/>
  </div>
</template>

<script>
import { securityRoleUsersDel, saveSecRole, secRole } from '../../../api/user-management'
import SecurityUser from './user-add/index'
export default {
  name: 'ResourceManagement',
  components: {
    SecurityUser
  },
  mixins: [

  ],
  props: {
  },
  data() {
    return {
      dataObj: {
        isShow: false
      },
      treeLoading: false,
      searchForm: {
        dscr: '',
        roleName: ''
      },
      currentPage: 1,
      pageSize: 15,
      total: 0,
      startRow: 0,
      endRow: 0,
      tableLoading: false,
      tableData: [],
      rules: {
        roleName: [{
          required: true, message: '请输入角色名称', trigger: 'blur'
        }]
      },
      roleId: window.sessionStorage.getItem('securityRoleId'),
      nodeInfo: {},
      selectedList: [],
      selectedListArr: []
    }
  },
  computed: {

  },
  watch: {

  },
  created() {

  },
  mounted() {

  },
  methods: {
    // 选择关联用户
    handleSelectionChange(val) {
      this.selectedList = val
      this.selectedListArr = this.selectedList.map(item => {
        return {

          roleId: this.roleId,
          roleName: this.searchForm.roleName,
          roleType: '1',
          ...item
        }
      })
    },
    // 删除用户
    delUser() {
      if (this.selectedListArr.length === 0) {
        this.$alert(`<div class="myalert-header">操作失败</div>
                    <div class="myalert-content">请至少选择一条用户</div>`, {
          dangerouslyUseHTMLString: true, confirmButtonText: '确定',
          type: 'warning'
        })
        return
      }
      this.$confirm(`此操作将永久删除角色【${this.searchForm.roleName}】下的${this.selectedList[0].uact}等账号, 是否继续?`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = this.selectedListArr
        securityRoleUsersDel(params).then(res => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              dangerouslyUseHTMLString: true,
              message: `<strong>操作成功</strong><p>${res.message}</p>`,
              duration: 3000
            })
            this.getRoleInfoList(this.roleId)
          } else {
            this.$alert(`<div class="myalert-header">操作失败</div>
                    <div class="myalert-content">${res.message}</div>`, {
              dangerouslyUseHTMLString: true, confirmButtonText: '确定',
              type: 'error'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增关联用户
    addNewUser() {
      if (!this.roleId) {
        this.$alert(`<div class="myalert-header">操作失败</div>
                    <div class="myalert-content">请选择一个业务角色</div>`, {
          dangerouslyUseHTMLString: true, confirmButtonText: '确定',
          type: 'error'
        })
        return
      }
      this.dataObj.isShow = true
      this.dataObj.roleId = this.roleId
      this.dataObj.searchForm = this.searchForm
    },
    // 获取节点参数
    getTreeNodeData(data) {
      this.nodeInfo = data
    },
    // 新增清空数据
    clearData() {
      // this.$refs.searchForm.resetFields()
      this.searchForm.roleName = '新增安全角色'
      this.searchForm.dscr = ''
      this.tableData = []
    },
    // 保存角色信息
    saveRoleInfo() {
      alert('000')
      if (!this.roleId) {
        let params = {
          admrolId: '',
          admrolType: '1',
          lv: 1,
          prntPath: '/',
          prntRoleId: this.nodeInfo.data.id,
          roleName: this.searchForm.roleName,
          dscr: this.searchForm.dscr
        }
        saveSecRole(params).then(res => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              dangerouslyUseHTMLString: true,
              message: `<strong>操作成功</strong><p>${res.message}</p>`,
              duration: 3000
            })
            this.searchForm = res.data
            this.$emit('reloadNode')
          } else {
            this.$alert(`<div class="myalert-header">操作失败</div>
            <div class="myalert-content">${res.message}</div>`, {
              dangerouslyUseHTMLString: true, confirmButtonText: '确定',
              type: 'error'
            })
          }
        })
      }
    },
    // 获取角色详情
    getRoleInfo(id) {
      secRole(id).then(res => {
        if (res.code === 0) {
          this.searchForm = res.data
        }
      })
    },
    // 获取角色列表
    getRoleInfoList(id) {
      this.tableLoading = true
      secRole(id).then(res => {
        if (res.code === 0) {
          this.tableData = res.data
          this.total = res.data.recordCount
          const num1 = this.pageSize * (this.currentPage - 1) + 1
          const num2 = this.pageSize * this.currentPage > this.total ? this.total : this.pageSize * this.currentPage
          this.startRow = num1
          this.endRow = num2
          this.tableLoading = false
        }
      })
    },
    changeIcon(icon) {
      this.searchForm.resuIcon = icon
    },
    cancelDialog() {
      this.dataObj.isShow = false
      this.getRoleInfoList(this.roleId)
    },
    restSearch() {

    }
  }
}
</script>

<style scoped lang="scss">
  .height100b{
    height: 100%;
  }
  .menu-maintenance{
//     /deep/ .fas-icon-list {
//     list-style-type: none;
//     margin: 0;
//     padding: 0;
//     display: flex;
//     flex-direction: row;
//     flex-wrap: wrap;
//     li {
//     width: 30px;
//     height: 30px;
//     margin: 5px;
//     // list-style: none;
//     // display: flex;
// }
// }
    /deep/.el-icon-popper {
    max-height: 400px;
    overflow: auto;
    overflow-x: hidden;
    overflow-y: hidden;
    }
    /deep/.el-input-group__prepend{
      span{
        display: inline-block;
        width:40px;
        height: 100%;
        cursor: pointer;
        text-align: center;
        i{
          display: inline-block;
          width:100%;
          height: 100%;
        }
      }
    }
  }

</style>
